package com.hyl.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.hyl.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000120606625";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCPLzCmHTE60xz+AYhQhCkhbidnl8JGdzx294idjriqUeuSrk4kBuStK2sqiBQ0htXcR06OGYhdE+GHSPfHwz2I+UHAbOPUnAI2OpZ5s26gVS2jnXrO0hxIqNaHFc3hhzGH7V3GNLkuMTaqesGcfVK/rxTklQSJEH89Ww6Luq7Au44wOqsHVTwUwRoqPl5qJz1sSuxsp2BCVBF8WUoQBaFz6giWKyAdcUI7ycumcuEHdSMn7d9CPkAvaLTbeMnryW/UqHscVgHAlpOGj4pF3cbzaNwElq3yvVIz6RVio7IlIwLeu9NfJbhGrXF1OGOp8k485IW9gYT8HHrMLcR9mKajAgMBAAECggEAEUQwiqeUizJwGZVXudXmG9jruYtMazDvwqzg9ZcDX86XKM4KMEAAjX1h4/Qa86aa3YBf0bT0g7zlAmpjLty1a/Ia4rh+1OGnRqDUCoL/wSfkZAmWiKA/tuddT1bqx9qEt46BM2iiOBbJL8+zz0oxk1nqnBH4RQsPPPfhDiqJ7kW/t38d7CeX2HNlEmY/AtnaTP4dCk9ZwomStyZ0hHA3Se1Y4q3bi+LrAawAExLuEoIeOk3BEg6cGMJLbuFjfxNZPcsVrlRujOy4HOZuIbCr5kuXj0XWdjahUFgOiuBu7wAIRyBGOfzuNT7EPgpyLppZO7emKG8R7llvGa4EnbbSKQKBgQD+XspvScvbFYTn3fH8vpOgXlhAb2uJTBYV5wqpxkqJIymsb5Bhe20XxEXVDuj8xqqtHc2jm62ppD7bfYsHNNq8zOoP4zAMvmIxp3WSUJL8siUjU/vImqEUTX6GAirNjkrBHuH9gMAewVO5tfvSdpvMdFepQKqIKcbpKUiTSAvH5wKBgQCQGgk19zYhJkK5DwA+x7KsWU/+i2zhTH5GMVZpQZOVq7jZ1XgLccS2QUHj5h+S5pIjfTfNhXhBkME22MSTwy6RTgaKyvHGtTLC7rrD5ivNsIzWK/GesaQUeHaU2nbTcH4p3u4aFFNje+zMKSN868wprEZxhilZviyT1dhjMyvj5QKBgG7tn5JQuFneVItZOsJhybdR7WKMNHU3fRooG3pWQwLzFqVgMbKRRM3shheX3Pr/GEdOi/MhXOZx/gdyBEMsRYe4RQqQpM+p5uCIirk38iHI/nEdmtusszpLYTXHlCr4Nplpglwu3ZmL7Zs7RYpOtzq+McxZqwSeU2Qy4Ld538lZAoGAeQDj6yUDlyKBtNQb6JvYNVFqEt0ROL766gZUAV7Hh08Dkifm/fUysbhqk+yRZtGmtZZYXYROTwA2vs+XglzLpchXRgTUsX9GeNnuisGk1rwCB3YvFosktOvS53vre/Fif9ZrKgsXWtqdJXMGEcgPUI8ROBLoditzU1Cmu+YkXOECgYA7vzSkyVOBqT1LQn9a0wYSzv23GPcIXyBAra0PWPd645E6ju6BfV85q9OsPQVy8CyN5AHbesd6MPa7gNvT2JeM1+UuuKqUAec27YxCwJKNpvD0MJR1kG3u1j/Q4p7Xw7Ql3y6GuWbpSm/eJ1MjusRp3zTUch9K9zR8UP7xMmy56w==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmpkTkI6G+3bPlrxGOx/PZPnPE1BVNAFag+bCFj4W0BcuA2hPXuXB4zg07XlKRSYgI7M06ZjqSjcaDlpHBqWBWd/6BgKku2gilqREoT9SHU/De1wSj2QzOcklPTIcWAEIVpFDvhQpqBamWoZBvdtubPu016xw9vN0mQ38o4a6fhsoKIA+1Rjugkj3Q3BaLsinnhzk1xb6ozhAOqBPNJ+KKJBqlE2WTlfXohkwa1txNAra5Wyl7X9O20QQfVYQkow4zGHeaqQt4uxf2awN65S/gY+H6CbhAps7M0yrP1v3Bd4nRCQpIFnNaWRHS6L3meO5ZKFuFBe9wsn3cTh3KOa3MQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://53a73y7619.wicp.vip/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页(这里是会员服务的会员订单页）
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    //支付时间限制
    private String time_out = "10m";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"" + time_out + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
